package com.freedom.study.escontroller;

import java.io.IOException;
import java.net.UnknownHostException;

import org.apache.ibatis.annotations.Delete;
import org.hibernate.type.TrueFalseType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.freedom.study.comresp.ReturnResponse;
import com.freedom.study.model.EsReq;
import com.freedom.study.service.IEsProService;
import com.freedom.study.service.IEsService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * Created by Thinkpad on 2017/11/14.
 */
@Api(tags = "ElasticSearch测试用例,最简单的增删改查")
@RestController
@RequestMapping(value = "/es/")
public class EsProController {

	@Autowired
	private IEsProService esService;
	
    @ApiOperation(value = "获取ElasticSearch数据，可模糊可分页", notes = "根据条件获取ElasticSearch数据")
    @GetMapping("getEsComplexData")
    public ReturnResponse getEsComplexData(@RequestParam(value="article",required =true) String article,@RequestParam(value="author",required =true) String author) {
    	ReturnResponse returnResponse = null;
			returnResponse = esService.getEsComplexData(article,author);
        return returnResponse;
    }

    @ApiOperation(value = "批量新增ElasticSearch数据", notes = "批量新增ElasticSearch数据")
	@ApiImplicitParams({
		@ApiImplicitParam(paramType = "header", value = "Authorization", name = HttpHeaders.AUTHORIZATION, dataType = "String")
	})
    @PostMapping(value = "addEsComplexData")
    public ReturnResponse addEsComplexData(@RequestBody EsReq req) {
    	ReturnResponse returnResponse = null;
    	returnResponse = esService.addEsComplexData(req);
        return returnResponse;
    }


    @ApiOperation(value = "更新部分ElasticSearch数据", notes = "更新部分ElasticSearch数据")
    @ApiImplicitParams({
		@ApiImplicitParam(paramType = "header", value = "Authorization", name = HttpHeaders.AUTHORIZATION, dataType = "String")
	})
    @PutMapping(value = "updateEsComplexData")
    public ReturnResponse updateEsComplexData(@RequestBody EsReq req) {
    	ReturnResponse returnResponse = null;
    	returnResponse = esService.updateEsComplexData(req);
        return returnResponse;
    }

    @ApiOperation(value = "删除批量ElasticSearch数据", notes = "根据条件删除批量ElasticSearch数据")
    @DeleteMapping("delEsCompelxData")
    public ReturnResponse delEsCompelxData(@RequestParam(value="article",required =true) String article,@RequestParam(value="author",required =true) String author) {
    	ReturnResponse returnResponse = null;
    	returnResponse = esService.delEsCompelxData(article,author);
        return returnResponse;
    }

}
